package org.jeecg.glr.iframe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.SysConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SysUtil;
import org.jeecg.glr.entity.GlrLightPole;
import org.jeecg.glr.entity.GlrSensor;
import org.jeecg.glr.entity.GlrVideo;
import org.jeecg.glr.iframe.service.IGlrIframeVideoMonitoringService;
import org.jeecg.glr.iframe.util.AjaxJson;
import org.jeecg.glr.service.IGlrSensorDataService;
import org.jeecg.glr.service.IGlrSensorService;
import org.jeecg.glr.service.IGlrVideoService;
import org.jeecg.glr.service.IGlrVideoYsService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.jeecg.common.constant.CommonConstant.St_Access_Token;

/**
 *iframe使用
 * 传感器图表
 */
@Slf4j
@Controller
@RequestMapping("/iframeGlr/sensorChart")
public class GlrIframeSensorChart extends JeecgController<GlrVideo, IGlrVideoService> {

    @Autowired
    private IGlrIframeVideoMonitoringService glrIframeVideoMonitoringService;

    @Autowired
    private IGlrSensorService glrSensorService;

    @Autowired
    private IGlrVideoYsService glrVideoYsService;

    @Autowired
    private IGlrSensorDataService glrSensorDataService;

    @Value("${glr.path.vue_url}")
    private String vue_url;

    /**
     * 传感器图表
     * @param modelAndView
     * @return
     */
    @RequestMapping("/index")
    public String list(Model model, HttpServletRequest request) throws Exception {
        try{
            LoginUser loginUser=glrIframeVideoMonitoringService.updateUserInfo(request);
            if (loginUser==null){
                return "redirect:"+vue_url;
            }
        }catch (Exception ex){
            return "redirect:"+vue_url;
        }
        model.addAttribute("AccessToken",request.getParameter(St_Access_Token));
        return "/glrTemplates/SensorChart/index";
    }


    /**
     * 取所有传感器
     * @param model
     * @return
     */
    @RequestMapping("/getSensorList")
    @ResponseBody
    public AjaxJson getSensorList(HttpServletRequest request, String type) throws Exception {
        AjaxJson j = new AjaxJson();
        LoginUser loginUser= glrIframeVideoMonitoringService.updateUserInfo(request);
        QueryWrapper queryWrapper = new QueryWrapper();
        String userType= loginUser.getUserType();
        if(!userType.equals(SysConstant.SYS_USER_TYPE_ADMIN)&&!userType.equals(SysConstant.SYS_USER_TYPE_0)){
            queryWrapper.eq(SysConstant.SYS_TABLE_COMPANY_CODE,loginUser.getSysCompanyCode());
        }
        if (StringUtil.isNotEmpty(type)){
            queryWrapper.eq("type",type);
        }
        List<GlrSensor> glrSensorList=glrSensorService.list(queryWrapper);

        List<Map<String, Object>> select = new ArrayList<>();
        for(GlrSensor glrSensor:glrSensorList){
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("code",glrSensor.getCode());
            resultMap.put("name",glrSensor.getName());
            select.add(resultMap);
        }
        j.setObj(select);
        return j;
    }


    /**
     * 取当前传感器最新的数据
     * @param model
     * @param code
     * @return
     */
    @RequestMapping("/getSensorChartData")
    @ResponseBody
    public AjaxJson getSensorChartData(HttpServletRequest request, String code) throws Exception {
        LoginUser loginUser= glrIframeVideoMonitoringService.updateUserInfo(request);

        String stSql="select glr_sensor.type as type,sensor_code as sensorCode,rainfall,humidity,airpressure,wind_Speed as windSpeed, " +
                "wind_Direction as windDirection,temperature,optical_Radiation as opticalRadiation,ultraviolet_Ray as ultravioletRay,\n" +
                "noise,pm25,pm10,glr_sensor_data.create_time as createDate \n" +
                "from glr_sensor_data \n" +
                "left join glr_sensor on glr_sensor.code=glr_sensor_data.sensor_code \n"+
                "where glr_sensor_data.create_time = (select MAX(create_time) from glr_sensor_data where sensor_code='"+code+"')";

        if(loginUser!=null){
            if(!loginUser.getUserType().equals(SysConstant.SYS_USER_TYPE_0)&&!loginUser.getUserType().equals(SysConstant.SYS_USER_TYPE_ADMIN)){
                stSql=stSql+" and glr_sensor_data.sys_company_code='"+ loginUser.getSysCompanyCode()+"'";
            }
        }

        List<Map<String, Object>> dataList=glrVideoYsService.doGetListForJdbc(stSql);

        AjaxJson j = new AjaxJson();

        if (dataList.size()>0){
            Object windDirection=dataList.get(0).get("windDirection");
            if (StringUtil.isNotEmpty(windDirection)){
                String st1=glrSensorDataService.getWindDirection(windDirection.toString());
                dataList.get(0).put("windDirection",st1);
            }else{
                dataList.get(0).put("windDirection","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("rainfall"))){

            }else{
                dataList.get(0).put("rainfall","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("airpressure"))){

            }else{
                dataList.get(0).put("airpressure","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("windSpeed"))){

            }else{
                dataList.get(0).put("windSpeed","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("opticalRadiation"))){

            }else{
                dataList.get(0).put("opticalRadiation","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("ultravioletRay"))){

            }else{
                dataList.get(0).put("ultravioletRay","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("pm10"))){

            }else{
                dataList.get(0).put("pm10","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("pm25"))){

            }else{
                dataList.get(0).put("pm25","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("noise"))){

            }else{
                dataList.get(0).put("noise","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("temperature"))){

            }else{
                dataList.get(0).put("temperature","- -");
            }

            if (StringUtil.isNotEmpty(dataList.get(0).get("humidity"))){

            }else{
                dataList.get(0).put("humidity","- -");
            }

            j.setObj(dataList.get(0));
        }else {
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("rainfall", "- -");
            resultMap.put("humidity", "- -");
            resultMap.put("airpressure", "- -");
            resultMap.put("temperature", "- -");
            resultMap.put("windSpeed", "- -");
            resultMap.put("windDirection", "- -");
            resultMap.put("noise", "- -");
            resultMap.put("pm25", "- -");
            resultMap.put("pm10", "- -");
            resultMap.put("opticalRadiation", "- -");
            resultMap.put("ultravioletRay", "- -");
            j.setObj(resultMap);
        }

        return j;
    }




}
